<template>
  <div class="m-launch-order-list">
    <div class="m-launch-tab">
      <order-search :search-btns="searchStatus" @on-changeBtns="onChangeStatus"></order-search>
    </div>
    <div class="search-criteria">
      <div class="o">
        <span class="title">商家名称</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.company"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商家手机号</span>
        <div class="input dis-block">
          <w-input holder="请填写" rule="isMobile" error="请输入正确的手机号" v-model="params.contactPhone"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商品货号</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.goodsSerial"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">商品名称</span>
        <div class="input dis-block">
          <w-input holder="请填写" v-model="params.goodsName"></w-input>
        </div>
      </div>
      <div class="o">
        <span class="title">拼单开始时间</span>
        <div class="date dis-block">
          <w-date-picker language="ch" placeholder="起始时间" v-model="params.addTime"></w-date-picker>
        </div>
      </div>
      <div class="search-btn">
        <w-button @on-click="fetchLaunchGoodsListSearch">查询</w-button>
        <w-button type="option" style="margin-left: 30px" :disabled="!goodsList.recordList || goodsList.recordList.length == 0" @on-click="onExportGoodsList">导出EXCEL</w-button>
      </div>
    </div>
    <div class="clearfix">
      <div class="m-options">
        <w-checkbox label="all" v-model="all" @on-change="onChangeCheckedAll">
          <span style="padding: 0 20px 0 0px;">全部</span>
        </w-checkbox>
        <w-button type="option" style="margin-right: 20px" v-if="params.ruleStatus == 0" @on-click="onCancelReview">取消审核</w-button>
      </div>
      <div class="m-list">
        <w-table :columns="columns"></w-table>
        <div v-if="goodsList.recordList && goodsList.recordList.length">
          <div v-for="(goods, index) in goodsList.recordList" :key="index">
            <div class="table-list">
              <div class="tab-title">
                <div class="checkbox">
                  <w-checkbox :label="`goods${index}`" v-model="goods.checked" @on-change="_setListToAllCheck"></w-checkbox>
                </div>
                <div>
                  <span>商家机构号：{{goods.institutionNum}}</span>
                  <span>商家名称：{{goods.company}}</span>
                </div>
              </div>
              <table class="table">
                <tr>
                  <td>
                    <div class="goods-info clearfix">
                      <!-- 缺货标记：out-stock -->
                      <div class="goods-img fl">
                        <img v-lazy="goods.goodsMainPhotoPath" alt="">
                      </div>
                      <div class="goods-name fl">
                        <p class="p-line-hide">{{goods.goodsName}}</p>
                        <span>货号：{{goods.goodsSerial}}</span>
                      </div>
                    </div>
                  </td>
                  <td>
                    <span>{{goods.addTime}}</span>
                  </td>
                  <td>
                    <span>{{goods.crowdfundingCount || 0}}件</span>
                  </td>
                  <td>
                    <span>{{goods.checkOutCount || 0}}</span>
                  </td>
                  <td>
                    <span v-if="goods.ruleStatus == 4">拼单中</span>
                    <span v-if="goods.ruleStatus == -1">未通过</span>
                    <span v-if="goods.ruleStatus == 0">待审核</span>
                    <span v-if="goods.ruleStatus == 5 && goods.isAlready == 1">拼单成功</span>
                    <span v-if="goods.ruleStatus == 5 && goods.isAlready == 0">已结束</span>
                    <span v-if="goods.ruleStatus == 5 && goods.isAlready == -1">拼单失败</span>
                  </td>
                  <td>
                    <div class="t-under" v-if="params.ruleStatus == -1 || params.ruleStatus == 5" @click="onSetLaunchGoods(goods.id, goods)">再次拼单</div>
                    <div class="t-under" v-if="params.ruleStatus == 0" @click="onReviewGoods(goods.id, goods.goodsRuleId)">审核拼单</div>
                    <div class="t-under" @click="$router.push(`detail?three=${params.threeMenuCode}&goodsid=${goods.id}&ruleid=${goods.goodsRuleId}&status=${goods.ruleStatus}`)">查看详情</div>
                  </td>
                </tr>
              </table>
            </div> 
          </div>
        </div>
        <div style="text-align: center;margin: 20px" v-else>
          <p>暂无数据</p>
        </div>
      </div>

      <!-- 分页 -->
      <div class="fr" v-if="goodsList.pageCount > 1">
        <w-paging :current="goodsList.currentPage" :total="goodsList.pageCount" @on-page="onChangePage"></w-paging>
      </div>
    </div>

    <!-- 设置拼单 -->
    <w-panel-validate title="设置拼单规则" :panel-title="true" confirmText="提交审核" v-model="setLaunchPanel" :height="`600px`" @on-confirm="submitUpdatedGoodsSkuInfoHandler" @on-cancel="_cancelParams">
      <div class="fightSet">
        <div class="fightRow">
          <div class="title">拼单结束时间：</div>
          <div class="date">
            <w-date-picker language="ch" :min="localDate" v-model="addParams.fightGoodsEndTime"></w-date-picker>
          </div>
        </div>
        <div class="fightRow">
          <div class="title">商品类型：</div>
          <div class="radioGroup clearfix">
            <div class="radio fl">
              <w-radio :label="`1`" name="goodsNumType" v-model="addParams.goodsNumType">现货</w-radio>
            </div>
            <!--  若该商品不是现货商品，则不可设置拼单商品为现货 -->
            <div class="radio">
              <w-radio :label="`0`" name="goodsNumType" v-model="addParams.goodsNumType">期货</w-radio>
            </div>
          </div>
        </div>
        <div class="fightRow">拼单参数配置</div>
        <div class="fightRow">
          <div class="SKU">
            <table class="table">
              <thead>
                <th>颜色</th><th>颜色最低发货量</th><th>尺码</th><th>采购商最低起订量</th><th v-if="addParams.goodsNumType == '1'">商品数量</th>
              </thead>
              <!-- SKU组合 -->
              <sku-group v-for="(item, index) in addParams.goodsRulesDetailList" :goods-type="addParams.goodsNumType" :index="index" :key="index" :specs="item" :count="true"></sku-group>
            </table>
          </div>
        </div>
        <div class="fightRow">
          <div class="title">拼单发货总量：</div>
          <div class="date">
            <div class="launch-count">
              <w-input rule="isEmpty|onlyInt"  error="必填|格式错误" v-model="addParams.crowdfundingCount"></w-input>
            </div>
            件
          </div>
        </div>
      </div>
    </w-panel-validate>

    <!-- 审核拼单弹框 -->
    <w-panel-fixed-wrap title="拼单审核" :panel-title="true" confirmText="提交" v-model="reviewGoods" @on-confirm="onConfirm" @on-cancel="_clearPanel">
      <div class="m-review-panel">
        <div style="margin-bottom: 10px">
          <span class="title">审核结果：</span>
          <div class="o-select">
            <w-radio :name="`审核`" :label="0" v-model="reviewParams.goodsStatus">通过</w-radio>
            <w-radio :name="`审核`" :label="-5" v-model="reviewParams.goodsStatus">不通过</w-radio>
          </div>
        </div>
        <div v-if="reviewParams.goodsStatus == -5">
          <span class="title">原因：</span>
          <div class="textarea">
            <w-input type="textarea" holder="请填写原因" v-model="reviewParams.refuseCause"></w-input>
          </div>
        </div>
      </div>
    </w-panel-fixed-wrap>
  </div>
</template>

<script>
import OrderSearch from '../orderMange/components/orderSearch'
import SkuGroup from './components/SkuGroup'
import { apiGoodsMange } from 'js/api'
import { mapGetters } from 'vuex'
import { API } from 'js/const'
import { formatDate, joinQueryParamsToUrl } from 'js/util'

export default {
  data() {
    return {
      searchStatus: [
        {
          title: '待审核',
          status: 0
        },
        {
          title: '拼单中',
          status: 4
        },
        {
          title: '未通过',
          status: -1
        },
        {
          title: '已结束',
          status: 5
        }
      ],
      columns: [
        {title: '货品', width: '28%'},
        {title: '拼单开始时间', width: '14%'},
        {title: '拼单数量', width: '12%'},
        {title: '已拼单数量', width: '11%'},
        {title: '状态', width: '15%'},
        {title: '操作', width: '10%'}
      ],
      all: false, // 全选
      setLaunchPanel: false, // 设置拼单弹框
      reviewGoods: false, // 拼单审核弹框
      goodsList: {},
      params: {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        currentPage: 1,
        showCount: 20,
        ruleStatus: 0,
        id: '', // 商品id
        addTime: '',
        company: '',
        institutionNum: '',
        contactPhone: '',
        goodsName: '',
        goodsSerial: ''
      },
      addParams: {
        accessToken: '',
        threeMenuCode: this.$route.query.three || '',
        id: '',
        goodsRuleId: '', // 拼单规则
        fightGoodsEndTime: '',
        crowdfundingCount: '',
        goodsNumType: 1,
        goodsRulesDetailList: []
      },
      reviewParams: {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        id: '',
        goodsRuleId: '', // 拼单规则
        goodsStatus: 0, // 审核结果
        refuseCause: '',
        type: 'pindan'
      }
    }
  },
  computed: {
    ...mapGetters(['token']),
    localDate() {
      return formatDate(new Date(), 'yyyy-MM-dd')
    }
  },
  components: {
    'order-search': OrderSearch,
    'sku-group': SkuGroup
  },
  created() {
    this.fetchLaunchGoodsList()
  },
  methods: {
    /**
     * 更改当前状态
     */
    onChangeStatus(status) {
      this.params.ruleStatus = status
      this.params.currentPage = 1
      // 清空搜索条件
      this.params.goodsName = ''
      this.params.institutionNum = ''
      this.params.company = ''
      this.params.addTime = ''
      this.params.goodsSerial = ''
      this.params.contactPhone = ''
      this.params.id = ''
      this.fetchLaunchGoodsList()
    },
    /**
     * 搜索
     */
    fetchLaunchGoodsListSearch() {
      this.params.currentPage = 1
      this.fetchLaunchGoodsList()
    },
    /**
     * 获取拼单商品列表数据
     */
    fetchLaunchGoodsList() {
      apiGoodsMange.apiLaunchGoods(this.params).then(data => {
        this.goodsList = data.data || {}
        if (!this.goodsList.recordList || this.goodsList.recordList.length == 0) return
        this.goodsList.recordList.forEach(v => {
          this.$set(v, 'checked', this.all)
        })
      })
    },
    /**
     * 切换全选
     */
    onChangeCheckedAll(bool) {
      if (!this.goodsList.recordList || this.goodsList.recordList.length == 0) return
      if (bool) {
        this.goodsList.recordList.forEach(v => {
          v.checked = true
        })
      } else {
        this.goodsList.recordList.forEach(v => {
          v.checked = false
        })
      }
    },
    /**
     * 判断选中的个数,是否和展示的长度相同，true相同，则选中全部，false全部取消
     */
    _setListToAllCheck() {
      this.all = true
      // 如果商品列表中只要有一个是未选中状态，则全选按钮也为未选中状态
      this.goodsList.recordList.forEach((v) => {
        if (!v.checked) {
          this.all = false
        }
      })
    },
    /**
     * 取消审核
     */
    onCancelReview() {
      let goodsIds = []
      let _this = this
      if (!this.goodsList.recordList || this.goodsList.recordList.length == 0) return
      this.goodsList.recordList.forEach(v => {
        if (v.checked) {
          goodsIds.push(v.id)
        }
      })
      this.$modal({
        title: '提示',
        content: '确认取消审核？',
        onConfirm() {
          apiGoodsMange.apiLaunchGoodsCancel(_this.token, _this.$route.query.three, goodsIds).then(data => {
            _this.$toast('操作成功！', () => {
              _this.fetchLaunchGoodsList()
            })
          })
        }
      })
    },
    /**
     * 分页
     */
    onChangePage(page) {
      this.params.currentPage = page
      this.fetchLaunchGoodsList()
    },
    /**
     * 展示审核弹框
     */
    onReviewGoods(goodsid, goodsRuleId) {
      this.reviewGoods = true
      this.reviewParams.id = goodsid
      this.reviewParams.goodsRuleId = goodsRuleId
    },
    /**
     * 提交审核
     */
    onConfirm() {
      if (!this.reviewParams.refuseCause && this.reviewParams.goodsStatus == -5) {
        this.$toast('请填写审核不通过原因')
        return
      }
      apiGoodsMange.apiGoodsReview(this.reviewParams).then(data => {
        this.$toast('审核成功！', () => {
          this.fetchLaunchGoodsList()
          this.reviewGoods = false
          this._clearPanel()
        })
      })
    },
    /**
     * 清空弹框内容
     */
    _clearPanel() {
      this.reviewParams = {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        id: '',
        goodsStatus: 0, // 审核结果
        refuseCause: '',
        type: 'pindan'
      }
    },
    /**
     * 展示设置拼单弹框
     */
    onSetLaunchGoods(goodsid, goodsInfo) {
      this.setLaunchPanel = true
      this.addParams.fightGoodsEndTime = goodsInfo.fightGoodsEndTimeAgain // 展示年月日
      this.addParams.crowdfundingCount = goodsInfo.crowdfundingCount
      this.addParams.goodsNumType = goodsInfo.supplyMode
      this.addParams.goodsRulesDetailList = JSON.parse(goodsInfo.goodsGsp || '[]')
      this.addParams.id = goodsid
      this.addParams.goodsRuleId = goodsInfo.goodsRuleId
    },
    /**
     * 设置拼单规则---提交审核
     */
    submitUpdatedGoodsSkuInfoHandler() {
      let flag = true
      if (!this.addParams.fightGoodsEndTime) {
        this.$toast('请选择拼单时间')
        return
      }
      this.addParams.goodsRulesDetailList.forEach(item => {
        if (!item.specpropertyCrowdSum || item.specpropertyCrowdSum == '') {
          this.$toast('请输入颜色最低发货量')
          flag = false
          return
        }
        item.specpropertyList.forEach(spcs => {
          if (!spcs.specpropertySmallCount) {
            this.$toast('请输入采购商最低起订量')
            flag = false
          }
        })
      })
      if (!flag) return
      this.addParams.accessToken = this.token
      apiGoodsMange.apiLaunchGoodsSet(this.addParams).then(data => {
        this.$toast('新增拼单规则成功!', () => {
          this.fetchLaunchGoodsList()
          this.setLaunchPanel = false
          this._cancelParams()
        })
      })
    },
    /**
     * 清空弹框参数
     */
    _cancelParams() {
      this.addParams.fightGoodsEndTime = ''
      this.addParams.crowdfundingCount = ''
      this.addParams.goodsRulesDetailList = []
    },
    /**
     * 商品导出
     */
    onExportGoodsList() {
      const params = {
        accessToken: this.token,
        goodsStatus: this.params.ruleStatus,
        company: this.params.company,
        institutionNum: this.params.institutionNum,
        contactPhone: this.params.contactPhone,
        goodsName: this.params.goodsName,
        goodsSerial: this.params.goodsSerial,
        type: '1' // 拼单商品
      }
      apiGoodsMange.apiGoodsExport(params).then(resp => {
        window.location.href = joinQueryParamsToUrl(`${API}goods/Goods/export`, params)
      })
    }
  }
}
</script>

<style lang="scss">
.m-launch-order-list {
  .search-btn {
    .btn {
      width: 140px;
    }
  }
  .m-review-panel {
    textarea {
      height: 120px;
    }
    .w-input .error-tip {
      margin-top: 120px;
    }
  }
  .fightSet {
    .w-input .error-tip {
      margin-top: 26px;
      left: 5px;
    }
    .date-list li .message span {
      width: 30px !important
    }
  }
}
</style>

<style lang="scss" scoped>
@import '../../scss/_var.scss';
.t-under {
  text-decoration: underline;
  // margin-right: 18px;
  font-size: 12px;
  color: #3693FF;
  cursor: pointer;
  line-height: 20px;
}
.m-launch-tab {
  margin: 5px auto 30px;
}
.search-criteria {
  border-bottom: 1px solid $border-gray-light;
  .title {
    width: 90px;
    text-align: right;
    display: inline-block;
    margin-right: 10px;
    font-size: 12px;
  }
  .icon {
    padding: 0 10px;
  }
  .dis-block {
    display: inline-block;
  }
  .o {
    margin-right: 40px;
    margin-bottom: 20px;
    display: inline-block;
  }
  .select {
    width:142px;
    height:28px; 
  }
  .o-select {
    width: 170px;
    height: 28px;
  }
  .input {
    width: 170px;
    vertical-align: middle;
  }
  .order-options {
    margin: 48px auto 20px;
    text-align: center;
  }
  .search-btn {
    margin: 42px auto 20px;
    text-align: center;
  }
  .date {
    width: 170px;
    vertical-align: middle;
  }
}
.m-options {
  margin: 20px auto 10px 10px;
}
.table-list {
  margin-bottom: 20px;
  .tab-title {
    width: 100%;
    height:40px; 
    background:rgba(242,242,242,1);
    font-size: 12px;
    color: #313131;
    font-weight: 600;
    line-height: 40px;
    padding-left: 10px;
    border: 1px solid #e0e0e0;
    border-bottom: none;
    margin-top: 20px;
    position: relative;
    span {
      &:first-child {
        margin-left: 35px
      }
      &:last-child {
        margin-left: 20px;
      }
    }
    .checkbox {
      display: inline-block;
      position: absolute;
      top: -10px;
    }
  }
  .goods-info {
    padding: 10px;
  }
  .goods-img {
    width: 88px;
    height: 88px;
    margin-right: 14px;
    position: relative;
  }
  .goods-name {
    display: inline-block;
    position: relative;
    height: 88px;
    width: 60%;
    span {
      position: absolute;
      bottom: 0;
      left: 0;
    }
  }
  td {
    &:nth-child(1) {
      width: 28%;
    }
    &:nth-child(2) {
      width: 14%;
    }
    &:nth-child(3) {
      width: 12%;
    }
    &:nth-child(4) {
      width: 11%;
    }
    &:nth-child(5) {
      width: 15%;
      span {
        line-height: 24px;
        &:last-child {
          color: #606060
        }
      }
    }
    &:nth-child(6) {
      width: 10%;
    }
    &:nth-child(7) {
      width: 10%;
    }
  }
}
.m-review-panel {
  .title {
    width: 100px;
    text-align: right;
    display: inline-block;
    vertical-align: top;
    font-size: 12px;
  }
  .o-select {
    width: 220px;
    display: inline-block;
  }
  .textarea {
    width:326px;
    height:130px; 
    display: inline-block;
  }
}
.fightSet {
  // 拼单模态框
  .fightRow {
      margin-bottom: 20px;
    .title {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
    }
    .date {
      display: inline-block;
      width: 352px;
    }
    .radioGroup {
      width: 250px;
      display: inline-block;
      .radio {
        margin-right: 50px;
      }
    }
    .launch-count {
      display: inline-block;
      width: 118px;
      margin-right: 10px;
    }
  }
}
</style>
